﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace Doo.Configuation
{
    public class CustomControlSection : ConfigurationSection
    {
        [ConfigurationProperty("services", IsDefaultCollection = false)]
        public WebServiceCollection WebServices
        {
            get
            { return (WebServiceCollection)this["services"]; }
        }

        [ConfigurationProperty("controls", IsDefaultCollection = true)]
        public ControlCollection Controls
        {
            get
            { return (ControlCollection)this["controls"]; }
        }
    }

    public class ControlSection : ConfigurationSection
    {
        /// <summary>
        /// 控件类型
        /// </summary>
        public string Type
        {
            get { return (string)this["Type"]; }
        }


    }

    #region Base

    public class Element : ConfigurationElement
    {
        [ConfigurationProperty("name", DefaultValue = "", IsRequired = true, IsKey = true)]
        public virtual string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        public Element() { }

        public Element(string name) { Name = name; }
    }

    public class ElementCollection<T> : ConfigurationElementCollection where T : Element, new()
    {

        public override
            ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return
                    ConfigurationElementCollectionType.AddRemoveClearMap;
            }
        }

        protected override
            ConfigurationElement CreateNewElement()
        {
            return new T();
        }

        protected override ConfigurationElement CreateNewElement(string elementName)
        {
            return new T() { Name = elementName };
        }

        protected override Object
            GetElementKey(ConfigurationElement element)
        {
            return ((T)element).Name;
        }

        public new int Count
        {

            get { return base.Count; }

        }


        public T this[int index]
        {
            get
            {
                return (T)BaseGet(index);
            }
            set
            {
                if (BaseGet(index) != null)
                {
                    BaseRemoveAt(index);
                }
                BaseAdd(index, value);
            }
        }

        public int IndexOf(T url)
        {
            return BaseIndexOf(url);
        }

        public void Add(T url)
        {
            BaseAdd(url);

            // Add custom code here.
        }

        protected override void
            BaseAdd(ConfigurationElement element)
        {
            BaseAdd(element, false);
            // Add custom code here.
        }

        public void Remove(T url)
        {
            if (BaseIndexOf(url) >= 0)
                BaseRemove(url.Name);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void Clear()
        {
            BaseClear();
            // Add custom code here.
        }

    }


    #endregion

    #region WebService 集合

    /// <summary>
    /// 定义Web服务
    /// </summary>
    public class WebServiceElement : Element
    {
        [ConfigurationProperty("type", DefaultValue = "", IsRequired = true)]
        public string Type
        {
            get { return (string)this["type"]; }
            set { this["type"] = value; }
        }

        [ConfigurationProperty("path", DefaultValue = "", IsRequired = true)]
        public string Path
        {
            get { return (string)this["path"]; }
            set { this["path"] = value; }
        }

    }

    public class WebServiceCollection : ElementCollection<WebServiceElement>
    {
        /// <summary>
        /// Web服务名字空间
        /// </summary>
        [ConfigurationProperty("namespace")]
        public string NameSpace
        {
            get { return (string)base["namespace"]; }
            set { base["namespace"] = value; }
        }

        /// <summary>
        /// Web服务基准路径
        /// </summary>
        [ConfigurationProperty("basepath")]
        public string BasePath
        {
            get { return (string)base["basepath"]; }
            set { base["basepath"] = value; }
        }

        public WebServiceCollection()
        {
            base.AddElementName = "service";
        }
    }

    #endregion

    #region 依赖集合

    public enum DependenceType
    {
        WebService,
        JSLibrary,
        JSFile,
        Html,
        CSS,
        XML
    }

    /// <summary>
    /// 依赖项，包括Web服务、JS库、JS、Html、CSS、XML等
    /// 可以指定是否是强制依赖，即在创建控件前必须首先加载；或非强制，如CSS、XML，即使未加载完成也可以加载控件
    /// </summary>
    public class DependenceElement : Element
    {
        [ConfigurationProperty("type", DefaultValue = DependenceType.WebService)]
        public DependenceType Type
        {
            get { return (DependenceType)this["type"]; }
            set { this["type"] = value; }
        }
    }

    public class DependenceCollection : ElementCollection<DependenceElement>
    {

    }


    #endregion

    #region 控件集合

    /// <summary>
    /// 定义控件
    /// </summary>
    public class ControlElement : Element
    {
        [ConfigurationProperty("type", DefaultValue = "", IsRequired = true)]
        public string Type
        {
            get { return (string)this["type"]; }
            set { this["type"] = value; }
        }

        [ConfigurationProperty("path", DefaultValue = "", IsRequired = true)]
        public string Path
        {
            get { return (string)this["path"]; }
            set { this["path"] = value; }
        }

        [ConfigurationProperty("tagName", DefaultValue = "")]
        public string TagName
        {
            get { return (string)this["tagName"]; }
            set { this["tagName"] = value; }
        }

        [ConfigurationProperty("depends", IsDefaultCollection = true)]
        public DependenceCollection Dependences
        {
            get
            { return (DependenceCollection)this["depends"]; }
        }

        public ControlElement() { }

        public ControlElement(string elementName)
        {
            Name = elementName;
        }
    }

    public class ControlCollection : ElementCollection<ControlElement>
    {
        public ControlCollection()
        {
            base.AddElementName = "control";
        }

        /// <summary>
        /// 控件类型名字空间
        /// </summary>
        [ConfigurationProperty("namespace")]
        public string NameSpace
        {
            get { return (string)base["namespace"]; }
            set { base["namespace"] = value; }
        }

        /// <summary>
        /// 控件代码基准路径
        /// </summary>
        [ConfigurationProperty("basepath")]
        public string BasePath
        {
            get { return (string)base["basepath"]; }
            set { base["basepath"] = value; }
        }

        [ConfigurationProperty("domain")]
        public string Domain
        {
            get { return base["domain"] as string; }
            set { base["domain"] = value; }
        }

        [ConfigurationProperty("debug")]
        public bool Debug
        {
            get { return (base["debug"] as bool?) == true; }
            set { base["domain"] = value; }
        }

        /// <summary>
        /// 标签前缀
        /// </summary>
        [ConfigurationProperty("tagPrefix")]
        public string TagPreFix
        {
            get { return (string)base["tagPrefix"]; }
            set { base["tagPrefix"] = value; }
        }
    }

    #endregion
}
